class Solution:
    def minFallingPathSum(self, A: list) -> int:
        dp = [A[0][i] for i in range(len(A))]

        for row in range(1, len(A)):
            cache = [0 for _ in range(len(A))]
            for col in range(len(A)):
                i = col - 1 if col - 1 >= 0 else col
                j = col + 1 if col + 1 < len(A) else col
                cache[col] = A[row][col] + min(dp[i:j + 1])
            dp = cache

        return min(dp)
